package com.mjh.partners.controller;

import java.io.FileOutputStream;
import java.io.OutputStream;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.mjh.partners.pojo.ShowMe;
import com.mjh.partners.service.ShowMeService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import net.sf.json.JSONArray;

import sun.misc.BASE64Decoder;


@RequestMapping("/ShowMeController")
@Controller
public class ShowMeController {
  @Autowired
  private ShowMeService showMeService;
  @ResponseBody
  @RequestMapping(value="getShowByUserid",produces = {"application/json;charset=UTF-8"})
  public String getShowByUserid(int userid,HttpServletRequest request,HttpServletResponse response) throws  Exception{
    List<ShowMe> Lists=showMeService.getShowByUserid(userid);
    response.setContentType("text/html;charset=utf-8");
    JSONArray json = JSONArray.fromObject(Lists);  
    String result=json.toString();
    return result;
  }
  public String upload(String base64,Date dt,int userid) {
    String reslut = null;
    BASE64Decoder decoder = new BASE64Decoder();  
    try   {
        byte[] b = decoder.decodeBuffer(base64);  
        for(int i=0;i<b.length;++i)  
        {  
            if(b[i]<0)
            {
                b[i]+=256;  
            } 
        }
        SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMddHHmmss");
        String fileName = "SMimg_"+sdf.format(dt)+String.valueOf(userid)+".png";
        String imgFilePath = "C:\\picture132\\show\\"+fileName;
        reslut= "http://localhost:8080/Partners/picture132/show/"+fileName;
        
        OutputStream out = new FileOutputStream(imgFilePath);      
        out.write(b);  
        out.flush();  
        out.close();
        return reslut;
    }catch (Exception e) {
      // TODO: handle exception
      return "error";
    }
    
  }
  @ResponseBody
  @RequestMapping("addShow")
  public String addShow(int userid,String base64,String sTitle,String sContent,int sType){
    Date dt = new Date();
    SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMddHHmmss");
    String sImage=null;;
    ShowMe showme=new ShowMe();
    String nowString=sdf.format(dt);
    showme.setsType(sType);
    showme.setsTitle(sTitle);
    showme.setsContent(sContent);
    if(base64.length()>5) {
      sImage=upload(base64,dt,userid);
      showme.setsImage(sImage);
    }
    showme.setUserid(userid);
    showme.setsCreateTime(nowString);
    boolean flag=showMeService.addShowMe(showme);
    if(flag) {
      return "success";
    }else {
      return "erorr";
    }
    }
  @ResponseBody
  @RequestMapping("deleteS")
  public String deleteS(int sid) {
    boolean flag=showMeService.deleteShowMe(sid);
    if(flag) {
      return "success";
    }else {
      return "error";
    }
  }
}
